package com.yswee.mallnavi.model;

import android.text.TextUtils;
import android.widget.Toast;

import com.yswee.framework.Application;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITask;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.net.http.HttpFactory;
import com.yswee.framework.net.http.HttpType;
import com.yswee.framework.net.http.PostType;
import com.yswee.mallnavi.Preference;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.context.ContextConstant;
import com.yswee.mallnavi.entity.common.CompanyEntity;

import java.io.File;
import java.net.HttpURLConnection;
import java.util.HashMap;

public abstract class BaseModel<T> extends com.yswee.framework.model.BaseModel<T> {
	
	public static final String CACHE_FOLDER_JSON = "json";

	protected String getCacheFilePath() {
		if(TextUtils.isEmpty(getCacheFileName())) {
			return null;
		}
		return CACHE_FOLDER_JSON + File.separator + getCacheFileName();
	}
	
	protected String getCacheFileName() {
		return null;
	}
	
	public BaseModel(ITaskContext context) {
		super(context);
	}
	
	protected void postData(CharSequence url, HashMap<String, Object> param, Callback<T> c) {
		postData(url, param, getParser(), c);
	}
	
	protected void postData(CharSequence url, HashMap<String, Object> param, Callback<T> c, String debug) {
		postData(url, param, getParser(), c, debug);
	}
	
	protected void postData(CharSequence url, HashMap<String, Object> param, CallbackProxy<T> c) {
		postData(url, param, getParser(), c);
	}
	
	protected <D> void postData(CharSequence url, HashMap<String, Object> param, IParser<D> parser, Callback<D> c) {
		postData(url, param, parser, c, null);
	}
	
	protected <D> void postData(CharSequence url, HashMap<String, Object> param, IParser<D> parser, Callback<D> c, final String debug) {
		CallbackProxy<D> callback = new CallbackProxy<D>(c) {
			protected String getDebugJson() {
				return debug;
			};
		};
		postData(url, param, parser, callback);
	}
	
	protected <D> void postData(CharSequence url, HashMap<String, Object> param, IParser<D> parser, CallbackProxy<D> callback) {
		Entity<D> entity = new Entity<D>();
		entity.setContext(getContext());
		entity.setBaseUrl(getWrapUrlPost(url.toString()));
		entity.setHttpType(HttpType.Post);
		entity.setPostData(getWrapPostData(param));
		entity.setPostType(PostType.JSON);
		entity.setParser(parser);
		ITask task = HttpFactory.get().createHttp(entity, callback);
		task.excute();
	}
	
	protected void getData(CharSequence url, Callback<T> c) {
		getData(url, getParser(), c, null);
	}
	
	protected void getData(CharSequence url, Callback<T> c, String debug) {
		getData(url, getParser(), c, debug);
	}
	
	protected void getData(CharSequence url, CallbackProxy<T> c) {
		getData(url, getParser(), c);
	}
	
	protected <D> void getData(CharSequence url, IParser<D> parser, Callback<D> c) {
		getData(url, parser, c, null);
	}
	
	protected <D> void getData(CharSequence url, IParser<D> parser, Callback<D> c, final String debug) {
		CallbackProxy<D> callback = new CallbackProxy<D>(c) {
			@Override
			protected String getDebugJson() {
				return debug;
			}
		};
		getData(url, parser, callback);
	}
	
	protected <D> void getData(CharSequence url, IParser<D> parser, CallbackProxy<D> callback) {
		Entity<D> entity = new Entity<D>();
		entity.setContext(getContext());
		entity.setBaseUrl(getWrapUrlGet(url.toString()));
		entity.setHttpType(HttpType.Get);
		entity.setParser(parser);
		ITask task = HttpFactory.get().createHttp(entity, callback);
		task.excute();
	}
	
	protected String getWrapUrlGet(String url) {
		return url;
	}
	
	protected String getWrapUrlPost(String url) {
		return url;
	}
	
	protected HashMap<String, Object> getWrapPostData(HashMap<String, Object> param) {
		return param;
	}
	
	public static class CallbackProxy<T> extends com.yswee.framework.model.BaseModel.CallbackProxy<T> {

		public CallbackProxy(Callback<T> callback) {
			super(callback);
		}
		
		@Override
		public void onBeginConnection(HttpURLConnection connection) {
			super.onBeginConnection(connection);
			CompanyEntity company = Preference.getCompany();
			if(company != null) {
				connection.addRequestProperty("__tenant", company.id);
				if (company.sites != null && company.sites.size() > 0) {
					connection.addRequestProperty("SiteId", company.sites.get(0).id);
				}
			}
			connection.addRequestProperty("ActionToken", ContextConstant.ACTIONTOKEN);
			connection.addRequestProperty("device", ContextConstant.DEVICETYPE);
			connection.addRequestProperty("v", ContextConstant.VERSION);
		}
		
		@Override
		public void onError(Entity<T> entity) {
			if(entity.getEntityStatus() == Entity.NETINVALID) {
				Toast.makeText(Application.get(), R.string.network_invalid, Toast.LENGTH_SHORT).show();
			}
			super.onError(entity);
		}
		
	}

}
